import { ts,  rule } from 'qvog-dsl';
import path from 'path';

const SEVERITY = 2;
const DOC_PATH = 'docs/file-naming-convention-check.md';
const DESCRIPTION = 'The `.js` files are recommended to use camelCase, while `.ets` and `.ts` files are recommended to use PascalCase. For resource files, it is suggested to use either camelCase or lowercase with underscores in the naming convention.';

function isCamelCase(str: string): boolean {
    return /^[a-z]+([A-Z][a-z]*)*$/.test(str);
}

function isPascalCase(str: string): boolean {
    return /^[A-Z][a-z]*([A-Z][a-z]*)*$/.test(str);
}

export default rule()
    .match((node): node is ts.EndOfFileToken => {
        return node.kind === ts.SyntaxKind.EndOfFileToken;
    })
    .when((node) => {
        let filePath = node.getSourceFile().fileName;
        let extname = path.extname(filePath);
        if (extname === '.ts' || extname === '.ets') {
            return !isPascalCase(path.basename(filePath, path.extname(filePath)));
        } else if (extname === '.js') {
            return !isCamelCase(path.basename(filePath, path.extname(filePath)));
        }
        return false;
    })
    .report({
        severity: SEVERITY,
        description: DESCRIPTION,
        docPath: DOC_PATH,
    });
